/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.alibaba.excel.EasyExcel;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Atmr;
import me.zhengjie.modules.system.domain.AtmrExamType;
import me.zhengjie.modules.system.domain.vo.ExamInfoVo;
import me.zhengjie.modules.system.listener.UploadExamInfoListener;
import me.zhengjie.modules.system.service.AtmrExamTypeService;
import me.zhengjie.modules.system.service.AtmrService;
import me.zhengjie.modules.system.service.ExamInfoService;
import me.zhengjie.modules.system.service.dto.AtmrExamTypeQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zhaoxiancheng
* @date 2021-11-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "题型管理管理")
@RequestMapping("/api/atmrExamType")
public class AtmrExamTypeController {

    private final AtmrExamTypeService atmrExamTypeService;

    @Autowired
    AtmrService atmrService;

    @Autowired
    ExamInfoService examInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('atmrExamType:list')")
    public void download(HttpServletResponse response, AtmrExamTypeQueryCriteria criteria) throws IOException {
        atmrExamTypeService.download(atmrExamTypeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询题型管理")
    @ApiOperation("查询题型管理")
    @PreAuthorize("@el.check('atmrExamType:list')")
    public ResponseEntity<Object> query(AtmrExamTypeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(atmrExamTypeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增题型管理")
    @ApiOperation("新增题型管理")
    @PreAuthorize("@el.check('atmrExamType:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AtmrExamType resources){
        return new ResponseEntity<>(atmrExamTypeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改题型管理")
    @ApiOperation("修改题型管理")
    @PreAuthorize("@el.check('atmrExamType:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AtmrExamType resources){
        atmrExamTypeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除题型管理")
    @ApiOperation("删除题型管理")
    @PreAuthorize("@el.check('atmrExamType:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        atmrExamTypeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/saveBatch")
    @Log("批量更新题型管理")
    @ApiOperation("批量更新题型管理")
    @PreAuthorize("@el.check('atmrExamType:edit')")
    public ResponseEntity<Object> createBatch(@Validated @RequestBody List<AtmrExamType> resources){
        if(CollectionUtils.isEmpty(resources)){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        for (AtmrExamType atmrExamType : resources){
            if(atmrExamType.getId() == null){
                atmrExamTypeService.create(atmrExamType);
            } else {
                atmrExamTypeService.update(atmrExamType);
            }
        }
        Atmr atmr = new Atmr();
        atmr.setId(Integer.valueOf(resources.get(0).getAtmrId()));
        Integer count = resources.stream().mapToInt(s -> Integer.valueOf(s.getExamNum())).sum();
        atmr.setExamCount(String.valueOf(count));
        atmrService.update(atmr);
        return new ResponseEntity<>(resources,HttpStatus.CREATED);
    }

    @Log("获取题型对应得试题列表")
    @ApiOperation("获取题型对应得试题列表")
    @PostMapping("/create/examInfo")
    public ResponseEntity<Object> createExamInfo(@RequestParam Integer atmrExamTypeId,@RequestParam Long examDatabaseId) throws IOException {
        return new ResponseEntity<>(examInfoService.createExamInfo(atmrExamTypeId,examDatabaseId),HttpStatus.OK);
    }

}